<?php
//
//  VictoryCMS - Content managment system and framework.
//  Copyright (C) 2009  Lewis Gunsch
//
//  This file is part of VictoryCMS.
//  
//  VictoryCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  VictoryCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with VictoryCMS.  If not, see <http://www.gnu.org/licenses/>.

/**
 * VictoryCMS - TestPageBuilder
 * 
 * @license http://www.gnu.org/licenses/gpl.html
 * @author Lewis Gunsch
 * @filesource
 * @package PageBuilders
 */

/**
 * This PageBuilder is for testing DisplayablePages. It will by default
 * print out a list of DisplayablePages, which can then be rendered.
 * 
 * @package PageBuilders
 */
class TestPageBuilder extends PageBuilder {

	private $messages; 
	
	public function __construct() {
		$this->messages = array();
	}

	/**
	 * @see include/PageBuilder#addMessage($message)
	 */
	public function addMessage($message) {
		array_push($this->messages, $message);
	}

	/**
	 * @see include/PageBuilder#assemblePage($page)
	 */
	public function assemblePage(DisplayablePage $page) {

	}

	/**
	 * @see include/PageBuilder#render()
	 */
	public function render() {

		// Get the page to display or display the homepage
		if (! empty($_GET["page"])) {
			$load_page = trim($_GET["page"]);
		} else {
			$load_page = "PageListing";
		}
		
		// Retrieve the page requested
		$pageCreator = new PageFactory();
		$currentPage = PageFactory::createDisplayablePage($load_page);
		if ( !is_object($currentPage) ) {
			trigger_error("Page requested could not be found!", E_USER_WARNING);
		}
		if (! $currentPage instanceof DisplayablePage) {
			trigger_error("The requested page could not be displayed!", E_USER_WARNING);
		}
		
		// Get the information from the page
		$currentPage->pagePreProcess();
		$html = $currentPage->renderPage( "/index.php?page=$load_page"  );
		$title = $currentPage->getTitle();
		$headerInfo = $currentPage->getHeaderInfo();
		
		$messageStr = "";
		foreach($this->messages as $key => $value) {
			$messageStr .= "$value<br />";
		}
		
		// Now build the page
		$my_string = <<<TESTPAGEBUILDER
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
	<title>Test PageBuilder for testing components and pages -- $title</title>
	$headerInfo
</head>
<body>
<div id="Page_Container"> 	
	<p><a href="/?page=PageListing">Page Listing</a></p>
	<hr />
	<strong>Messages:</strong> <br />
	$messageStr
	<hr />
	$html
	
</div>
</body>
</html>
TESTPAGEBUILDER;

		echo $my_string;
	}
}

?>